import unittest

from Autotests.BaseTest import BaseManservSOAPTestCase, PartnerAndAccountAutoCreation


@PartnerAndAccountAutoCreation
class Products(BaseManservSOAPTestCase):
    def __init__(self, *args, **kwargs):
        BaseManservSOAPTestCase.__init__(self, *args, **kwargs)

    def setUp(self, *args, **kwargs):
        BaseManservSOAPTestCase.setUp(self, *args, **kwargs)
        self._baseProduct = {
            'id': 100,
            'name': 'Auto Test Product',
            'partnerId': int(self.getPartnerForCaseInfo().id),
            'features': {
                'pair': [
                    {
                        'first': 'AdvancedScheduling',
                        'second': '1'
                    },
                    {
                        'first': 'AllowFilters',
                        'second': '1'
                    }],
            }
        }

    def test_AddProduct(self):
        count = self._getProductCount()
        result = self.manserv.AddProduct(**self._baseProduct)
        self.assertTrue(result.isnumeric())
        countAfter = self._getProductCount()
        self.assertEqual(count + 1, countAfter)

        self.manserv.RemoveProduct(productId = result)

    def test_EnumerateProducts(self):
        productId = self.manserv.AddProduct(**self._baseProduct)
        result = self.manserv.EnumerateProducts(partnerId = self.getPartnerForCaseInfo().id,
                                                currentPartnerOnly = True)
        product = result.products.productInfo[0]
        listOfFeatures = self._baseProduct['features']['pair']
        #verify
        self.assertEqual(product.id, int(productId))
        self.assertEqual(product.name, self._baseProduct['name'])
        self.assertEqual(product.partnerId, int(self.getPartnerForCaseInfo().id))
        self.assertEqual(product.features.pair[0].first, listOfFeatures[0]['first'])
        self.assertEqual(product.features.pair[0].second, listOfFeatures[0]['second'])
        self.assertEqual(product.features.pair[1].first, listOfFeatures[1]['first'])
        self.assertEqual(product.features.pair[1].second, listOfFeatures[1]['second'])
        self.assertEqual(len(product.features.pair), len(listOfFeatures))

        resultAll = self.manserv.EnumerateProducts(partnerId = self.getPartnerForCaseInfo().id,
                                                currentPartnerOnly = False)
        product = resultAll.products.productInfo[0]
        #verify
        self.assertEqual(product.id, int(productId))
        self.assertEqual(product.name, self._baseProduct['name'])
        self.assertEqual(product.partnerId, int(self.getPartnerForCaseInfo().id))
        self.assertEqual(product.features.pair[0].first, listOfFeatures[0]['first'])
        self.assertEqual(product.features.pair[0].second, listOfFeatures[0]['second'])
        self.assertEqual(product.features.pair[1].first, listOfFeatures[1]['first'])
        self.assertEqual(product.features.pair[1].second, listOfFeatures[1]['second'])
        self.assertEqual(len(product.features.pair), len(listOfFeatures))

        self.manserv.RemoveProduct(productId = productId)

    def test_ModifyProduct(self):
        listOfFeatures = [
            {
                'first': 'AdvancedScheduling',
                'second': '1'
            },
            {
                'first': 'SearchInRestoreWindow',
                'second': '1'
            },
            ]

        productId = self.manserv.AddProduct(**self._baseProduct)
        modifiedProduct = self._baseProduct.copy()
        modifiedProduct['id'] = int(productId)
        modifiedProduct['name'] = 'A new auto product'
        modifiedProduct['features']['pair'] = listOfFeatures

        result = self.manserv.ModifyProduct(**modifiedProduct)
        self.assertIsNone(result)
        product = self._getLastProduct()
        self.assertEqual(product.id, int(productId))
        self.assertEqual(product.name, modifiedProduct['name'])
        self.assertEqual(product.partnerId, int(self.getPartnerForCaseInfo().id))
        self.assertEqual(product.features.pair[0].first, listOfFeatures[0]['first'])
        self.assertEqual(product.features.pair[0].second, listOfFeatures[0]['second'])
        self.assertEqual(product.features.pair[1].first, listOfFeatures[1]['first'])
        self.assertEqual(product.features.pair[1].second, listOfFeatures[1]['second'])
        self.assertEqual(len(product.features.pair), len(listOfFeatures))

        self.manserv.RemoveProduct(productId = productId)

    def test_RemoveProduct(self):
        productId = self.manserv.AddProduct(**self._baseProduct)
        count = self._getProductCount()
        result = self.manserv.RemoveProduct(productId = productId)
        self.assertIsNone(result)
        countAfter = self._getProductCount()
        self.assertEqual(count - 1, countAfter)

#---
    def _getProductCount(self):
        result = self.manserv.EnumerateProducts(partnerId = self.getPartnerForCaseInfo().id, currentPartnerOnly = False)
        if result == '':
            return 0
        return len(result.products.productInfo)

    def _getLastProduct(self):
        result = self.manserv.EnumerateProducts(partnerId = self.getPartnerForCaseInfo().id, currentPartnerOnly = True)
        return result.products.productInfo[0]


if __name__ == "__main__":
    unittest.main()
